package com.xfp.controller;

import com.xfp.utils.AliyunOSSUtils;
import com.xfp.utils.AliyunOssProperties;
import com.xfp.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/api/vm-service/sku/fileUpload")
@Slf4j
@Api(tags = "文件上传到阿里云")
public class UploadController {

    @Autowired
    private AliyunOssProperties ali;

    /**
     * 上传图片到阿里云
     * @param fileName
     * @return
     * @throws Exception
     */
    @PostMapping
    @ApiOperation("上传头像")
    public String upload(MultipartFile fileName) throws Exception {
        log.info("文件为：{}",fileName);
        String fileOri = fileName.getOriginalFilename();
        String suffix = fileOri.substring(fileOri.lastIndexOf("."));
        String fileName2 = UUID.randomUUID()+suffix;
        String upload = AliyunOSSUtils.upload(ali.getEndPoint(), ali.getBucketName(), fileName.getBytes(), fileName2);
        return upload;
    }

}
